package com.doghome.news.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.doghome.news.dao.CommentMapper;
import com.doghome.news.entity.Comment;
import com.doghome.news.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentMapper commentMapper;
	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public PageInfo<Comment> selectByPage(Map<String, Object> params) {
		Integer pageNum = Integer.valueOf(params.get("pageNum").toString()) ;
		Integer pageSize = Integer.valueOf(params.get("pageSize").toString()) ;
		PageHelper.startPage(pageNum,pageSize);
		Integer nid = params.containsKey("nid") ? Integer.valueOf(params.get("nid").toString()) : null;
		String ccontent = params.containsKey("ccontent") ? params.get("ccontent").toString() : null;
		
		return new PageInfo<Comment>(commentMapper.selectAll(nid, ccontent));
	}
	@Override
	public boolean addComment(Comment comment) {
		comment.setCdate(new Date());
		int count = commentMapper.insert(comment);
		if(count ==  1) {
			return true;
		}
		return false;
	}
	@Override
	public int delCommentByNid(int nid) {
		return commentMapper.deleteByNid(nid);
	}

}
